# -*- coding: utf-8 -*-

# @Time : 2022/6/14 20:14

# @Author : scy

# @File : __init__.py.py

# @Software: PyCharm
import random

from interact import interactSql


class manage:
    # 初始化
    def __int__(self):
        self.__account = None
        self.__password = None

    # 登录函数
    def login(self, maccount, mpassword):
        self.__account = maccount
        self.__password = mpassword

        # 从数据库中取出数据查询是否能登录
        try:
            conn = interactSql()
            conn.connect_sql()
            conn.cursor_sql()
            # 获取密码等信息
            conn.run_sql('select * from administrator where account = "%s" ' % self.__account)
            staff_message = conn.get_results()
            # 判断是否存在
            if len(staff_message) == 0:
                return '管理员不存在不存在'
            # 符合，登录成功
            if staff_message[0][1] == self.__password:
                # 赋值
                (self.__account, self.__password) = staff_message[0]
                return '登陆成功'
            else:
                return '密码错误，请重新输入'
        except Exception as e:
            if 'conn' in dir():
                print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')

    # 增加工作人员
    def add_worker(self, sstaffname, spassword='', repeat_password='', saccount=''):
        if spassword != repeat_password:
            return '两次密码不相同'
        try:
            conn = interactSql()
            conn.connect_sql()
            conn.cursor_sql()
            # 如果没有指定工号随机生成一个 格式 字母 + 数字
            if saccount == '':
                # 随机生成一个工号
                flag = random.randint(0, 1)
                if flag == 1:  # 小写字母
                    letter = chr(random.randint(0, 26) + ord('a'))
                else:
                    letter = chr(random.randint(0, 26) + ord('A'))
                saccount = letter + str(random.randint(0, 9))

            conn.run_sql('insert into staff(staffname, account, password) values ("%s", "%s", "%s")' % (
                sstaffname, saccount, spassword))
            conn.commit()
            return '创建成功'
        except Exception as e:
            if 'conn' in dir():
                conn.rollback()
                print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')

    # 插入相亲地点
    def add_place(self, plocation, pstaff, sum=0):

        try:
            conn = interactSql()
            conn.connect_sql()
            conn.cursor_sql()

            conn.run_sql(
                'insert into place(location, staff, people_sum) values ("%s", "%s", "%s")' % (plocation, pstaff, sum))
            conn.commit()
            return '地点创建成功'
        except Exception as e:
            if 'conn' in dir():
                conn.rollback()
                print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')

    # 修改地点信息，暂不支持修改地址
    def modify_palce(self, plocation, pstaff='', sum=0):

        try:
            conn = interactSql()
            conn.connect_sql()
            conn.cursor_sql()
            # 对输入情况进行判断
            if sum != 0 and pstaff == '':
                conn.run_sql('update place set people_sum = %d where location = "%s"' % (sum, plocation))
            elif sum == 0 and pstaff != '':
                conn.run_sql('update place set staff = "%s" where location = "%s"' % (pstaff, plocation))
            elif sum != 0 and pstaff != '':
                conn.run_sql(
                    'update place set people_sum = %d, staff = "%s" where location = "%s"' % (sum, pstaff, plocation))
            conn.commit()
            return '地点修改成功'
        except Exception as e:
            if 'conn' in dir():
                conn.rollback()
                print(e)
            return '错误，请联系工作人员'
        finally:
            if 'conn' in dir():
                conn.close_database('cursor')
                conn.close_database('connect')
